﻿using IBLL;
using IDAL;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class OrderDetailBLL : IOrderDetailBLL
    {
        private IOrderDetailDAL _orderDetailDAL;
        private IOrderInfoDAL _orderInfoDAL;
        private IProductInfoDAL _productInfoDAL;
        private IShopInfoDAL _shopInfoDAL;
        private ICustomerInfoDAL _customerInfoDAL;
        private ICustomerAddressDAL _customerAddressDAL;

        public OrderDetailBLL(
            IOrderDetailDAL orderDetailDAL,
            IOrderInfoDAL orderInfoDAL,
            IProductInfoDAL productInfoDAL,
            IShopInfoDAL shopInfoDAL,
            ICustomerInfoDAL customerInfoDAL,
            ICustomerAddressDAL customerAddressDAL

            )
        {
            _orderDetailDAL = orderDetailDAL;
            _orderInfoDAL = orderInfoDAL;
            _productInfoDAL = productInfoDAL;
            _shopInfoDAL = shopInfoDAL;
            _customerInfoDAL = customerInfoDAL;
            _customerAddressDAL = customerAddressDAL;
        }

        #region 获取订单结算的方法 (GetOrderDetailH)
        /// <summary>
        /// 获取订单结算的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetOrderDetailDTO> GetOrderDetailDTO(int page, int limit, out int count)
        {
            var data = from od in _orderDetailDAL.GetEntities()
                       select new GetOrderDetailDTO
                       {
                           Id = od.Id,
                           CreateTime = od.CreateTime
                       };
            count = data.Count();

            var listpage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();

            return listpage;

        }
        #endregion

        #region 获取订单结算的方法 (GetOrderDetailH)
        /// <summary>
        /// 获取订单结算的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetShopInfoDTO> GetOrderDetailH(int page, int limit, out int count)
        {
            var data = from s in _shopInfoDAL.GetEntities()
                       join c in _customerInfoDAL.GetEntities()
                       on s.CustomerId equals c.Id
                       into SCtemp
                       from sc in SCtemp.DefaultIfEmpty()
                       join p in _productInfoDAL.GetEntities()
                       on s.ProductId equals p.Id
                       into SPTemp
                       from sp in SPTemp.DefaultIfEmpty()
                       select new GetShopInfoDTO
                       {
                           Id = s.Id,
                           ProductImage = sp.Image,
                           ProductPrice = sp.ProductPrice,
                           ProductName = sp.ProductName,
                           PurchaseNumber = s.PurchaseNumber,
                           CustomerName = sc.CustomerName,
                           CustomerAddress = sc.AddressId,
                           CreateTime = s.CreateTime
                       };
            count = data.Count();

            var listpage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();

            return listpage;

        }
        #endregion

        #region 按顾客获取订单结算的方法 (GetOrderDetails)
        /// <summary>
        /// 按顾客获取订单结算的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="customerId"></param>
        /// <param name="orderId"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetShopInfoDTO> GetOrderDetail(int page, int limit, string customerId, string orderId, out int count)
        {

            var data = from s in _shopInfoDAL.GetEntities().Where(it => it.CustomerId == customerId && it.IsDelete == true && it.OrderId == orderId)

                       join od in _orderDetailDAL.GetEntities()
                       on s.ProductId equals od.Id
                       into SODemp
                       from sod in SODemp.DefaultIfEmpty()

                       join c in _customerInfoDAL.GetEntities().Where(it => it.Id == customerId)
                       on s.CustomerId equals c.Id
                       into SCtemp
                       from sc in SCtemp.DefaultIfEmpty()

                       join p in _productInfoDAL.GetEntities().Where(u => u.IsDelete == false)
                       on s.ProductId equals p.Id
                       into SPTemp
                       from sp in SPTemp.DefaultIfEmpty()
                       select new GetShopInfoDTO
                       {
                           Id = s.Id,
                           ProductImage = sp.Image,
                           ProductPrice = sp.ProductPrice,
                           ProductName = sp.ProductName,
                           PurchaseNumber = s.PurchaseNumber,
                           CustomerName = sc.CustomerName,
                           CustomerAddress = sc.AddressId,
                           CreateTime = s.CreateTime
                       };

            count = data.Count();

            var listpage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();

            return listpage;
        }
        #endregion

        #region 创建订单的方法 (CreateOrderDetail)
        /// <summary>
        /// 创建订单的方法
        /// </summary>
        /// <param name="odet">用户实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        public bool CreateOrderDetail(OrderDetail odet, out string msg)
        {
            OrderInfo orderInfo = _orderInfoDAL.GetEntities().FirstOrDefault(it => it.Id == odet.OrderId);
            if (orderInfo == null)
            {
                msg = "订单号为空";
                return false;
            }
            if (_orderDetailDAL.GetEntities().FirstOrDefault(it => it.OrderId == odet.OrderId) != null)
            {
                msg = "订单已经存在";
                return false;
            }
            odet.Id = Guid.NewGuid().ToString();//用户id
            odet.CreateTime = DateTime.Now;//设置时间
            odet.OrderId = orderInfo.Id;
            bool IsSuccess = _orderDetailDAL.CreateEntity(odet);//调用方法
            msg = IsSuccess ? $"添加成功" : "添加失败";

            return IsSuccess;
        }
        #endregion

        #region 删除订单的方法 (DeleteOrderDetail)
        /// <summary>
        /// 删除的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteOrderDetail(string id)
        {
            OrderDetail odet = _orderDetailDAL.GetEntities().FirstOrDefault(it => it.Id == id);
            if (odet == null)
            {
                return false;
            }
            _orderDetailDAL.DeleteEntity(odet);
            return true;
        }
        #endregion

        #region 批量删除订单的方法 (DeleteListOrderDetail)
        /// <summary>
        /// 批量删除的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteListOrderDetail(List<string> ids)
        {
            List<OrderDetail> odetList = _orderDetailDAL.GetEntities().Where(it => ids.Contains(it.Id)).ToList();
            foreach (var item in ids)
            {
                OrderDetail odet = _orderDetailDAL.GetEntities().FirstOrDefault(it => it.Id == item);
                if (odetList == null)
                {
                    return false;
                }
                _orderDetailDAL.DeleteEntity(odet);
            }
            return true;
        }
        #endregion

        #region 订单结算的方法 (UpdateOrderDetail)
        /// <summary>
        /// 订单结算的方法
        /// </summary>
        /// <param name="odet"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdateOrderDetail(string productName, int purchaseNumber,string customerInfoId,int customerMonty, out string msg)
        {
            CustomerInfo customerInfo = _customerInfoDAL.GetEntityById(customerInfoId);
            if (customerInfo == null)
            {
                msg = "无法查找到该顾客";
                return false;
            }
            if (customerInfo.CustomerMonty - customerMonty < 0)
            {
                msg = "金额不足";
                return false;
            }
            customerInfo.CustomerMonty -= customerMonty;


            ProductInfo productInfo = _productInfoDAL.GetEntities().FirstOrDefault(it => it.ProductName == productName);
            if(productInfo.ProductNum - purchaseNumber < 0)
            {
                msg = "商品库存不足";
                return false;
            }
            productInfo.ProductNum = productInfo.ProductNum - purchaseNumber;

            bool IsSuccess = _productInfoDAL.UpdateEntity(productInfo);//调用方法
            IsSuccess = _customerInfoDAL.UpdateEntity(customerInfo);

            msg = IsSuccess ? $"修改成功" : "修改失败";

            return IsSuccess;
        }
        #endregion

        #region 根据ID获取数据返回赋值 (GetOrderDetailById)
        /// <summary>
        /// 根据ID获取数据返回赋值
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OrderDetail GetOrderDetailById(string id)
        {
            return _orderDetailDAL.GetEntities().FirstOrDefault(it => it.Id == id);
        }
        #endregion

    }
}
